package absc.editor;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;
import org.eclipse.ui.part.FileEditorInput;

import absc.object.SWOTObject;
import absc.util.Constantes;

public class SWOTEditorDesignPage extends EditorPart implements SelectionListener{

	private Button btAdicionarS, btRemoverS, btEditarS, btAdicionarW, btRemoverW, btEditarW, btAdicionarO, btRemoverO, btEditarO, 
	btAdicionarT, btRemoverT, btEditarT;
	private SWOTObject swot;	
	private IWorkbenchWindow window;
	private Table tabStrenghts, tabWeaknesses, tabOpportunities, tabThreats;
	
	@Override
	public void doSave(IProgressMonitor arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doSaveAs() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init(IEditorSite editorSite, IEditorInput editorInput) throws PartInitException {
		FileEditorInput fei;		
		
		window = editorSite.getWorkbenchWindow();
		
		fei = (FileEditorInput)editorInput;
		swot = new SWOTObject();
		swot.loadSWOT(fei.getPath().toOSString());
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void createPartControl(Composite parent) {
		Composite tela, parteSuperior, parteGrid, parteBotoesGrid;
		GridLayout layoutTela, layoutParteGrid;
		GridData layoutTabela, layoutBotao;		
		TableColumn colItem, colCheck;
		CLabel lblSWOT;
		
		layoutTela = new GridLayout();
		layoutTela.numColumns = 1;
		layoutTela.makeColumnsEqualWidth = true;
		
		tela = new Composite(parent, SWT.NONE);
		tela.setLayout(layoutTela);
		
		parteSuperior = new Composite(tela, SWT.APPLICATION_MODAL);
		parteSuperior.setLayout(layoutTela);
		
		lblSWOT = new CLabel(parteSuperior, SWT.CENTER);
		lblSWOT.setText("SWOT Analysis");
		lblSWOT.setFont(new Font(Display.getDefault(), "Tahoma", 18, SWT.NORMAL));
			
		layoutParteGrid = new GridLayout();
		layoutParteGrid.numColumns = 4;
		layoutParteGrid.makeColumnsEqualWidth = true;
				
		parteGrid = new Composite(tela, SWT.APPLICATION_MODAL);
		parteGrid.setLayout(layoutParteGrid);
		
		layoutTabela = new GridData();
		layoutTabela.heightHint = 150;
		layoutTabela.widthHint = 200;
		layoutTabela.horizontalAlignment = SWT.CENTER;
		
		tabStrenghts = new Table(parteGrid, SWT.SINGLE | SWT.HIDE_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
		tabStrenghts.setHeaderVisible(true);
		tabStrenghts.setLinesVisible(true);
		tabStrenghts.setLayoutData(layoutTabela);
		tabStrenghts.setBounds(new org.eclipse.swt.graphics.Rectangle(10,20,200,150));
		
		colCheck = new TableColumn(tabStrenghts, SWT.NONE);
		colCheck.setWidth(30);
		colCheck.pack();
		
		colItem = new TableColumn(tabStrenghts, SWT.CENTER);
		colItem.setText("Strenghts");		
		colItem.setWidth(200);
		
		parteBotoesGrid = new Composite(parteGrid, SWT.NONE);
		parteBotoesGrid.setLayout(layoutTela);
		
		layoutBotao = new GridData();
		layoutBotao.widthHint = 50;
		
		btAdicionarS = new Button(parteBotoesGrid, SWT.NONE);
		btAdicionarS.setText("Add");
		btAdicionarS.setLayoutData(layoutBotao);
		btAdicionarS.addSelectionListener(this);
		
		btEditarS = new Button(parteBotoesGrid, SWT.NONE);
		btEditarS.setText("Edit");
		btEditarS.setLayoutData(layoutBotao);
		btEditarS.addSelectionListener(this);
		
		btRemoverS = new Button(parteBotoesGrid, SWT.NONE);
		btRemoverS.setText("Delete");
		btRemoverS.setLayoutData(layoutBotao);
		btRemoverS.addSelectionListener(this);

		tabWeaknesses = new Table(parteGrid, SWT.SINGLE | SWT.HIDE_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
		tabWeaknesses.setHeaderVisible(true);
		tabWeaknesses.setLinesVisible(true);
		tabWeaknesses.setLayoutData(layoutTabela);
		tabWeaknesses.setBounds(new org.eclipse.swt.graphics.Rectangle(330,20,200,150));

		colCheck = new TableColumn(tabWeaknesses, SWT.NONE);
		colCheck.setWidth(30);
		colCheck.pack();
		
		colItem = new TableColumn(tabWeaknesses, SWT.CENTER);
		colItem.setText("Weaknesses");
		colItem.setWidth(200);
		
		parteBotoesGrid = new Composite(parteGrid, SWT.NONE);
		parteBotoesGrid.setLayout(layoutTela);
		
		btAdicionarW = new Button(parteBotoesGrid, SWT.NONE);
		btAdicionarW.setText("Add");
		btAdicionarW.setLayoutData(layoutBotao);	
		btAdicionarW.addSelectionListener(this);
		
		btEditarW = new Button(parteBotoesGrid, SWT.NONE);
		btEditarW.setText("Edit");
		btEditarW.setLayoutData(layoutBotao);
		btEditarW.addSelectionListener(this);
		
		btRemoverW = new Button(parteBotoesGrid, SWT.NONE);
		btRemoverW.setText("Delete");
		btRemoverW.setLayoutData(layoutBotao);
		btRemoverW.addSelectionListener(this);
	
		tabOpportunities = new Table(parteGrid, SWT.SINGLE | SWT.HIDE_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
		tabOpportunities.setHeaderVisible(true);
		tabOpportunities.setLinesVisible(true);
		tabOpportunities.setLayoutData(layoutTabela);
		tabOpportunities.setBounds(new org.eclipse.swt.graphics.Rectangle(10,250,200,150));
		
		colCheck = new TableColumn(tabOpportunities, SWT.NONE);
		colCheck.setWidth(30);
		colCheck.pack();
		
		colItem = new TableColumn(tabOpportunities, SWT.CENTER);
		colItem.setText("Opportunities");
		colItem.setWidth(200);		
		
		parteBotoesGrid = new Composite(parteGrid, SWT.NONE);
		parteBotoesGrid.setLayout(layoutTela);
		
		btAdicionarO = new Button(parteBotoesGrid, SWT.NONE);
		btAdicionarO.setText("Add");
		btAdicionarO.setLayoutData(layoutBotao);
		btAdicionarO.addSelectionListener(this);
		
		btEditarO = new Button(parteBotoesGrid, SWT.NONE);
		btEditarO.setText("Edit");
		btEditarO.setLayoutData(layoutBotao);
		btEditarO.addSelectionListener(this);
		
		btRemoverO = new Button(parteBotoesGrid, SWT.NONE);
		btRemoverO.setText("Delete");
		btRemoverO.setLayoutData(layoutBotao);
		btRemoverO.addSelectionListener(this);
	
		tabThreats = new Table(parteGrid, SWT.SINGLE | SWT.HIDE_SELECTION | SWT.V_SCROLL | SWT.H_SCROLL);
		tabThreats.setHeaderVisible(true);
		tabThreats.setLinesVisible(true);
		tabThreats.setLayoutData(layoutTabela);
		tabThreats.setBounds(new org.eclipse.swt.graphics.Rectangle(330,250,200,150));
		
		colCheck = new TableColumn(tabThreats, SWT.NONE);
		colCheck.setWidth(30);
		colCheck.pack();

		colItem = new TableColumn(tabThreats, SWT.CENTER);
		colItem.setText("Threats");
		colItem.setWidth(200);
		
		parteBotoesGrid = new Composite(parteGrid, SWT.NONE);
		parteBotoesGrid.setLayout(layoutTela);
		
		btAdicionarT = new Button(parteBotoesGrid, SWT.NONE);
		btAdicionarT.setText("Add");
		btAdicionarT.setLayoutData(layoutBotao);
		btAdicionarT.addSelectionListener(this);
		
		btEditarT = new Button(parteBotoesGrid, SWT.NONE);
		btEditarT.setText("Edit");
		btEditarT.setLayoutData(layoutBotao);
		btEditarT.addSelectionListener(this);
		
		btRemoverT = new Button(parteBotoesGrid, SWT.NONE);
		btRemoverT.setText("Delete");
		btRemoverT.setLayoutData(layoutBotao);
		btRemoverT.addSelectionListener(this);
	}

	@Override
	public void setFocus() {
		List<String> lista;
		
		lista = swot.getStrenghts();
		populaGrid(lista, this.tabStrenghts);
		
		lista = swot.getWeaknesses();
		populaGrid(lista, this.tabWeaknesses);

		lista = swot.getOpportunities();
		populaGrid(lista, this.tabOpportunities);

		lista = swot.getThreats();
		populaGrid(lista, this.tabThreats);
	}

	private void populaGrid(List<String> lista, Table table) {
		TableItem ti;
		Text txtItem;		
		Button check;
		TableEditor te;
		
		table.removeAll();
		
		for(String tmp: lista){
			ti = new TableItem(table, SWT.NONE);
			
			check = new Button(table, SWT.CHECK);
			check.pack();

			te = new TableEditor(table);
			te.minimumWidth = check.getSize().x;
			te.horizontalAlignment = SWT.CENTER;
			te.setEditor(check, ti, 0);			
			
			txtItem = new Text(table, SWT.NONE);
			txtItem.setText(tmp);
			txtItem.pack();
			
			te = new TableEditor(table);
			te.minimumWidth = txtItem.getSize().x;
			te.horizontalAlignment = SWT.CENTER;
			te.setEditor(txtItem, ti, 1);
		}		
	}

	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub		
	}

	public void widgetSelected(SelectionEvent e) {
		int erro;
		
		erro = 0;
		
		if (e.getSource().equals(this.btAdicionarS)){
			openEditorItemPage(Constantes.STRENGHT, "");
		}
		else if (e.getSource().equals(this.btAdicionarW)){
			openEditorItemPage(Constantes.WEAKNESSES, "");
		}
		else if (e.getSource().equals(this.btAdicionarO)){
			openEditorItemPage(Constantes.OPPORTUNITIES, "");
		}
		else if (e.getSource().equals(this.btAdicionarT)){
			openEditorItemPage(Constantes.THREATS, "");
		}
		else if (e.getSource().equals(this.btEditarS)){
			String item = getItemEditado(Constantes.STRENGHT);
			
			if (item.equalsIgnoreCase(""))
				erro = 1;
			else if (item.equalsIgnoreCase("Error.varios"))
				erro = 2;
			else
				openEditorItemPage(Constantes.STRENGHT, item);
		}
		else if (e.getSource().equals(this.btEditarW)){
			String item = getItemEditado(Constantes.WEAKNESSES);
			
			if (item.equalsIgnoreCase(""))
				erro = 1;
			else if (item.equalsIgnoreCase("Error.varios"))
				erro = 2;
			else
				openEditorItemPage(Constantes.WEAKNESSES, item);
		}
		else if (e.getSource().equals(this.btEditarO)){
			String item = getItemEditado(Constantes.OPPORTUNITIES);
			
			if (item.equalsIgnoreCase(""))
				erro = 1;
			else if (item.equalsIgnoreCase("Error.varios"))
				erro = 2;
			else
				openEditorItemPage(Constantes.OPPORTUNITIES, item);
		}
		else if (e.getSource().equals(this.btEditarT)){
			String item = getItemEditado(Constantes.THREATS);
			
			if (item.equalsIgnoreCase(""))
				erro = 1;
			else if (item.equalsIgnoreCase("Error.varios"))
				erro = 2;
			else
				openEditorItemPage(Constantes.THREATS, item);
		}
		else if (e.getSource().equals(this.btRemoverS)){
			if (! apagaItem(Constantes.STRENGHT))
				erro = 3;			
		}		
		
		if (erro == 1)
			MessageDialog.openError(this.window.getShell(), "ABSC Error", "Um item deve ser marcado para ser editado.");
		else if (erro == 2)
			MessageDialog.openError(this.window.getShell(), "ABSC Error", "Apenas um item deve ser marcado para ser editado.");
		else if (erro == 3)
			MessageDialog.openError(this.window.getShell(), "ABSC Error", "Um item deve ser marcado para ser apagado.");		
	}

	private boolean apagaItem(int tipoItem) {
		boolean retorno;
		List<String> lista;
		
		retorno = false;
		lista = getItensMarcados(tipoItem);
		
		if (lista.size() > 0){
			swot.delItens(lista, tipoItem);
			retorno = true;
		}
		
		setFocus();
		
		return retorno;
	}

	private String getItemEditado(int tipoItem) {
		String item;
		List<String> lista;
		
		lista = getItensMarcados(tipoItem);
		
		if (lista.size() == 0)
			item = "";
		else if (lista.size() == 1)
			item = lista.get(0);
		else
			item = "Erros.varios";
		
		return item;
	}
	
	private List<String> getItensMarcados(int tipoItem){
		Table tabela;		
		List<String> lista;
		
		tabela = null;
		lista = new ArrayList<String>();
		
		if (tipoItem == Constantes.STRENGHT){
			tabela = this.tabStrenghts;
		}
		else if (tipoItem == Constantes.WEAKNESSES){
			tabela = this.tabWeaknesses;
		}
		else if (tipoItem == Constantes.OPPORTUNITIES){
			tabela = this.tabOpportunities;
		}
		else if (tipoItem == Constantes.THREATS){
			tabela = this.tabThreats;
		}
		
		for(int i = 0; i < tabela.getChildren().length; i = i + 2){
			Button check = (Button) tabela.getChildren()[i];
			if (check.getSelection()){
				Text txt = (Text) tabela.getChildren()[i + 1];
				lista.add(txt.getText());
			}
		}
		
		return lista;
	}

	private void openEditorItemPage(int tipoItem, String nomeItem) {
		SWOTEditorItemPage itemPage = new SWOTEditorItemPage(this.window.getShell(), tipoItem, this.swot, nomeItem, this);
		itemPage.open();
	}

	public void refresh() {
		swot.refresh();
		setFocus();
	}
}
